/*
 * GoodsListProvider.java
 * business
 *
 * Created by ChenTao on 2017/3/2.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.ui.goodsdetail.GoodsDetailActivity;
import com.yidiandao.business.ui.widget.dialog.QuickBuyDialog;
import com.yidiandao.business.ui.widget.view.FlagView;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.RegexUtils;
import com.yidiandao.business.utils.TextRenderUtils;
import com.yidiandao.business.utils.UIUtils;
import com.yidiandao.business.utils.UmengUtils;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 横向的商品列表
 * <p>
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/3/2 11:22
 */
public class GoodsListProvider extends ItemViewProvider<Goods, GoodsListProvider.ViewHolder> {

    private Activity mActivity;

    public GoodsListProvider(Activity activity) {
        mActivity = activity;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View rootView = inflater.inflate(R.layout.item_goods_list, parent, false);
        return new ViewHolder(rootView);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Goods goods) {
        refreshUI(holder, goods);
        initListeners(holder, goods);
    }

    private void initListeners(ViewHolder holder, final Goods goods) {
        holder.mRlItemInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 进入商品详情
                UmengUtils.onEvent(mActivity, R.string.umeng_home_to_goods_detail);
                GoodsDetailActivity.actionStart(mActivity, goods.getGoodsId());
            }
        });

        holder.ivShoppingCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                        // 未登录 --> 去登录
//                        if (!PriceUtils.isLogin()) {
//                            LoginActivity.actionStart(mActivity);
//                            return;
//                        }

                // 已登录，获取商品购物车信息
                getGoodsCartInfo(goods);
            }
        });

//        holder.tvGoodsPrice.setOnClickListener(
//                new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        // 未登陆点击价格-去登录
//                        if (!PriceUtils.isLogin()) {
//                            LoginActivity.actionStart(mActivity);
//                        }
//                    }
//                });
    }

    private void refreshUI(ViewHolder holder, Goods goods) {
        ImageLoader.loadImageWithPlaceHolder(mActivity, holder.ivGoodsImage, goods.getGoodsThumb());
        holder.tvGoodsName.setText(goods.getGoodsName());
        holder.flagView.setFlagText(goods.getFavourableNotice(), goods.getFavourableType());
        holder.tvShopName.setText(goods.getSupplierName());

        String goodsNumberDesc = goods.getGoodsNumberDesc();
        boolean isNumber = RegexUtils.isFloat(goodsNumberDesc);

        String singlePrice = goods.getAvgShopPriceFloat();
        if (RegexUtils.isFloat(singlePrice) && Float.parseFloat(singlePrice) == 0) {
            // 单瓶价等于0，不显示单瓶价格
            holder.tvSinglePrice.setVisibility(View.GONE);
        } else {
            // 显示单瓶价格
            holder.tvSinglePrice.setVisibility(View.VISIBLE);
            holder.tvSinglePrice.setText(getSingePrice(singlePrice));
        }

        holder.tvGoodsStock.setText(isNumber ? UIUtils.getString(R.string.stock_hint_unit, goodsNumberDesc, goods.getGoodsUnit())
                : UIUtils.getString(R.string.stock_hint_unit_1, goodsNumberDesc));
        holder.tvGoodsPrice.setText(TextRenderUtils.getBeautyPrice(goods.getShopPriceFloat(), goods.getGoodsUnit(), UIUtils.dp2px(18), UIUtils.dp2px(14)));

        int stockNumber = goods.getGoodsNumber();
        holder.tvSellOut.setVisibility(stockNumber > 0 ? View.GONE : View.VISIBLE);
        holder.ivShoppingCart.setVisibility(stockNumber > 0 ? View.VISIBLE : View.GONE);
    }

    private SpannableStringBuilder getSingePrice(String singlePrice) {
        SpannableStringBuilder builder = new SpannableStringBuilder();

        SpannableString pre = TextRenderUtils.getAbsoluteSizeText(UIUtils.getString(R.string.single_hint), UIUtils.dp2px(13));
        SpannableString suf = TextRenderUtils.getBeautyPrice(singlePrice, null, UIUtils.dp2px(15), UIUtils.dp2px(12));

        builder.append(pre);
        builder.append(suf);

        return builder;
    }

    private void getGoodsCartInfo(final Goods goods) {

        // 获取商品的购物车信息
        float goodsPrice = Float.parseFloat(goods.getShopPriceFloat());
        int goodsNumber = 1;
        int stockNumber = goods.getGoodsNumber();
        String goodsId = goods.getGoodsId();
        int limitNumber = goods.getLimitNumber();
        int minNumber = goods.getMinNumber();

        // 最少起订量大于库存,无法购买
        if (minNumber > stockNumber) {
            UIUtils.showToast(UIUtils.getString(R.string.can_not_buy_when_out_of_stock));
            return;
        }

        Goods newGoods = new Goods();
        newGoods.setGoodsId(goodsId);
        newGoods.setStockNumber(stockNumber);
        newGoods.setGoodsName(goods.getGoodsName());
        newGoods.setGoodsPrice(goodsPrice);
        newGoods.setGoodsNumber(goodsNumber);
        newGoods.setLimitNumber(limitNumber);
        newGoods.setMinNumber(minNumber);
        // 没有优惠活动，则显示立即立即购买，否则不显示立即购买
        newGoods.setFavourableNotice(goods.getFavourableNotice());

        showAddToCartDialog(newGoods);
    }

    private void showAddToCartDialog(Goods goods) {

        QuickBuyDialog dialog = new QuickBuyDialog(mActivity);
        dialog.show();

        dialog.setGoods(goods);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvGoodsName;
        private TextView tvSinglePrice;
        private TextView tvGoodsStock;
        private TextView tvGoodsPrice;
        private ImageView ivShoppingCart;
        private ImageView ivGoodsImage;
        private FlagView flagView;
        private RelativeLayout mRlItemInfo;
        private TextView tvShopName;
        private TextView tvSellOut;

        private ViewHolder(View itemView) {
            super(itemView);
            mRlItemInfo = (RelativeLayout) itemView.findViewById(R.id.rl_item_info);
            flagView = (FlagView) itemView.findViewById(R.id.flag_view);
            ivGoodsImage = (ImageView) itemView.findViewById(R.id.iv_goods_image);
            tvGoodsName = (TextView) itemView.findViewById(R.id.tv_goods_name);
            tvSinglePrice = (TextView) itemView.findViewById(R.id.tv_single_price);
            tvGoodsStock = (TextView) itemView.findViewById(R.id.tv_goods_stock);
            tvGoodsPrice = (TextView) itemView.findViewById(R.id.tv_goods_price);
            ivShoppingCart = (ImageView) itemView.findViewById(R.id.iv_shopping_cart);
            tvShopName = (TextView) itemView.findViewById(R.id.tv_shop_name);
            tvSellOut = (TextView) itemView.findViewById(R.id.tv_sell_out);
        }
    }
}
